<template>
    <view>
        <mescroll-uni
            ref="mescrollRef"
            @init="mescrollInit"
            :height="status == -1 || status == 1 ? height : sublistHeight"
            :down="downOption"
            @down="downRefresh"
            :up="upOption"
            @up="getData">
            <view v-if="show">
                <order-item
                    v-for="(item, index) in datas"
                    @selectNum="selectNum"
                    :status="status"
                    :saleStatus="saleStatus"
                    :key="item.ID"
                    :data="item"></order-item>
            </view>
        </mescroll-uni>
        <block v-if="datas.length > 0">
            <view class="footer" v-if="status == 0 || status == 3">
                <view class="btn1" @click="all(allstatus)">{{ allselect }}</view>
                <image src="@/static/img/line.png"></image>
                <view class="btn2" @click="del(4)"
                    >删除<text v-if="num > 0">({{ num }})</text></view
                >
                <image src="@/static/img/line.png"></image>
                <view class="btn3" @click="submit(3)"
                    >提交审核<text v-if="num > 0">({{ num }})</text></view
                >
            </view>
            <view class="footer footer1" v-if="status == 2 && saleStatus == 1">
                <view class="btn1" @click="all(allstatus)">{{ allselect }}</view>
                <image src="@/static/img/line.png"></image>
                <view class="btn3" @click="shelf(2)"
                    >批量下架<text v-if="num > 0">({{ num }})</text></view
                >
            </view>
            <view class="footer footer1" v-if="status == 2 && saleStatus == 2">
                <view class="btn1" @click="all(allstatus)">{{ allselect }}</view>
                <image src="@/static/img/line.png"></image>
                <view class="btn3" @click="shelf(1)"
                    >批量上架<text v-if="num > 0">({{ num }})</text></view
                >
            </view>
        </block>
        <uni-popup ref="popTip" type="dialog">
            <uni-popup-dialog
                type="text"
                title="温馨提示"
                :content="title"
                :before-close="true"
                @close="cancelIt"
                @confirm="doIt"></uni-popup-dialog>
        </uni-popup>
    </view>
</template>
<script>
import { mapMutations } from "vuex"
import { ApplyProduct, DeleteProducts } from "@/pages_supplier/service/api/product.js"
import orderItem from "@/pages_supplier/components/product/order-item.vue"
import { GetProductListBySupplier, UpdateProductSaleStatus } from "@/pages_supplier/service/api/product.js"
export default {
    props: {
        height: {
            type: String,
            default: "100px",
        },
        sublistHeight: {
            type: String,
            default: "100px",
        },
        status: {
            type: Number,
            default: -1,
        },
        saleStatus: {
            type: Number,
            default: -1,
        },
        requestData: {
            type: Object,
            default: {},
        },
    },
    components: {
        orderItem,
    },
    data() {
        return {
            datas: [],
            data: [],
            productList: [],
            param: {},
            downOption: {
                use: true,
            },
            show: false,
            allselect: "全选",
            num: 0,
            typenum: 0,
            title: "",
            ids: [],
            allstatus: true,
        }
    },
    mounted() {
        //#ifndef MP
        this.refresh(this.requestData)

        //#endif
    },
    methods: {
        ...mapMutations(["UP_PRODUCT_VIEW"]),
        //获取列表数据
        getData(page) {
            let param = this.param
            param.pageSize = page.size
            param.pageIndex = page.num
            param.status = this.status
            if (this.saleStatus) {
                param.saleStatus = this.saleStatus
            }
            GetProductListBySupplier(param).then((res) => {
                if (res.State == 1) {
                    this.mescroll.endBySize(res.Data.length, res.RecordAmout)
                    res.Data.forEach((e) => {
                        e.checked = false
                    })
                    this.datas = this.datas.concat(res.Data)
                    this.show = true
                } else {
                    this.mescroll.endBySize(0, 0)
                }
            })
        },
        //刷新
        refresh(requestData) {
            this.downRefresh(requestData, true)
            this.$forceUpdate()
        },
        //刷新
        downRefresh(requestData, isSetParam = false) {
            this.datas = []
            if (isSetParam) {
                this.param = requestData
            }
            this.mescroll.resetUpScroll()
        },
        all(status) {
            this.datas.forEach((e) => {
                e.checked = status
                if (e.checked) {
                    this.allselect = "取消全选"
                } else {
                    this.allselect = "全选"
                }
            })
            this.allstatus = !status
            this.selectNum()
        },
        selectNum() {
            let num = 0
            this.ids = []
            this.datas.forEach((e) => {
                if (e.checked) {
                    num++
                    this.ids.push(e.ID)
                }
            })
            console.log(this.ids)
            this.num = num
            if (num < this.datas.length) {
                this.allselect = "全选"
                this.allstatus = true
            } else {
                this.allselect = "取消全选"
                this.allstatus = false
            }
        },
        del(num) {
            if (this.ids.length == 0) {
                this.easyToast("请先勾选商品")
            } else {
                this.typenum = num
                this.title = "确定要删除选中项？"
                this.$refs.popTip.open()
            }
        },
        submit(num) {
            if (this.ids.length == 0) {
                this.easyToast("请先勾选商品")
            } else {
                this.typenum = num
                this.title = "确定提交审核选中项？"
                this.$refs.popTip.open()
            }
        },
        shelf(num) {
            if (this.ids.length == 0) {
                this.easyToast("请先勾选商品")
            } else {
                this.typenum = num
                if (num == 2) {
                    this.title = "确定批量下架选中项？"
                } else {
                    this.title = "确定批量上架选中项？"
                }
                this.$refs.popTip.open()
            }
        },
        cancelIt() {
            this.$refs.popTip.close()
        },
        doIt() {
            if (this.typenum == 4) {
                //删除
                let path = "?ids=" + this.ids.join(",")
                this.cancelIt()
                DeleteProducts(path).then((res) => {
                    if (res.State == 1) {
                        this.easyToast("删除成功")
                        setTimeout((e) => {
                            this.UP_PRODUCT_VIEW(new Date().getTime())
                        }, 1000)
                    } else {
                        this.easyToast(res.Msg)
                    }
                })
            } else if (this.typenum == 3) {
                //提交审核
                let path = "?product_id=" + this.ids.join(",")
                this.cancelIt()
                ApplyProduct(path).then((res) => {
                    if (res.State == 1) {
                        this.easyToast("提交成功")
                        setTimeout((e) => {
                            this.UP_PRODUCT_VIEW(new Date().getTime())
                        }, 1000)
                    } else {
                        this.easyToast(res.Msg)
                    }
                })
            } else if (this.typenum == 2) {
                //批量下架
                let path = "?ids=" + this.ids.join(",") + "&state=" + this.typenum
                this.cancelIt()
                UpdateProductSaleStatus(path).then((res) => {
                    if (res.State == 1) {
                        this.easyToast("操作成功")
                        setTimeout((e) => {
                            this.UP_PRODUCT_VIEW(new Date().getTime())
                        }, 1000)
                    } else {
                        this.easyToast(res.Msg)
                    }
                })
            } else if (this.typenum == 1) {
                //批量上架
                let path = "?ids=" + this.ids.join(",") + "&state=" + this.typenum
                this.cancelIt()
                UpdateProductSaleStatus(path).then((res) => {
                    if (res.State == 1) {
                        this.easyToast("操作成功")
                        setTimeout((e) => {
                            this.UP_PRODUCT_VIEW(new Date().getTime())
                        }, 1000)
                    } else {
                        this.easyToast(res.Msg)
                    }
                })
            }
        },
    },
}
</script>

<style lang="scss">
.footer {
    width: 750rpx;
    height: 120rpx;
    background: #ffffff;
    box-shadow: 0px -2rpx 6rpx 0px rgba(0, 0, 0, 0.1);
    position: fixed;
    left: 0;
    bottom: 0;
    z-index: 10;
    font-size: 28rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    image {
        width: 4rpx;
        height: 50rpx;
    }
    view {
        width: 33%;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .btn2,
    .btn3 {
        color: $app-color-main;
    }
}
.footer1 {
    view {
        width: 50%;
    }
}
</style>
